<template>
  <view>
    <view class="warpv">
      mhs
    </view>
    <view>
      <input style="height: 20vw;" type="username" v-model="user.userName" placeholder="请输入账号" />
    </view>
    <view>
      <input style="height: 20vw;" type="password" v-model="user.password" placeholder="请输入密码" />
    </view>
    <view>
      <button @click="login">登陆/注册</button>
    </view>
    <u-loading-icon v-if="loading" text="登陆中..." textSize="18"></u-loading-icon>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        user: {
          userName: '',
          password: ''
        },
        loading: false
      }
    },
    methods: {
      login() {
        this.loading = true;
        uni.request({
          url: 'http://127.0.0.1:800/mhs/user/register',
          method: 'POST',
          data: this.user,
          success: (res) => {
            if (res.data.code == 1) {
              console.log('登陆成功');
              //获得token完成登录,并将token存入本地缓存
              uni.setStorageSync('token', res.data.data.token);
              //存入user
              uni.setStorageSync('loginUser', res.data.data.loginUser);
			  this.$websocket.state.token =  res.data.data.token;
              this.$websocket.commit('webSocketInit', res.data.data.loginUser.id);
			  this.$websocket.commit('setUid',res.data.data.loginUser.id);
              this.loading = false;
              uni.showToast({
                title: '登陆成功',
                duration: 2000,
                mask: true //是否有透明蒙层，默认为false 
              })
              uni.reLaunch({
                url: '../user/user'
              });
            }else{
              uni.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
              this.loading = false;
            }
          }
        })
      }
    }
  }
</script>

<style>
  .warpv {
    font-size: 25px;
    text-align: center;
    padding: 20px;
    text-shadow: 5px 3px 0px #FFC107;
  }
</style>